<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_users
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access.
defined('_JEXEC') or die;

jimport('joomla.application.component.controlleradmin');

/**
 * Users list controller class.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_users
 * @since       1.6
 */
class UsersControllerUsers extends JControllerAdmin
{
	/**
	 * @var    string  The prefix to use with controller messages.
	 * @since  1.6
	 */
	protected $text_prefix = 'COM_USERS_USERS';

	/**
	 * Constructor.
	 *
	 * @param   array  $config  An optional associative array of configuration settings.
	 *
	 * @return  UsersControllerUsers
	 *
	 * @since   1.6
	 * @see     JController
	 */
	public function __construct($config = array())
	{
		parent::__construct($config);

		$this->registerTask('block',		'changeBlock');
		$this->registerTask('unblock',		'changeBlock');
	}

	/**
	 * Proxy for getModel.
	 *
	 * @param   string  $name    The model name. Optional.
	 * @param   string  $prefix  The class prefix. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return  object  The model.
	 *
	 * @since	1.6
	 */
	public function getModel($name = 'User', $prefix = 'UsersModel', $config = array('ignore_request' => true))
	{
		return parent::getModel($name, $prefix, $config);
	}

	/**
	 * Method to change the block status on a record.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	public function changeBlock()
	{
		// Check for request forgeries.
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$ids	= JRequest::getVar('cid', array(), '', 'array');
		$values	= array('block' => 1, 'unblock' => 0);
		$task	= $this->getTask();
		$value	= JArrayHelper::getValue($values, $task, 0, 'int');

		if (empty($ids))
		{
			JError::raiseWarning(500, JText::_('COM_USERS_USERS_NO_ITEM_SELECTED'));
		}
		else
		{
			// Get the model.
			$model = $this->getModel();

			// Change the state of the records.
			if (!$model->block($ids, $value))
			{
				JError::raiseWarning(500, $model->getError());
			}
			else
			{
				if ($value == 1)
				{
					$this->setMessage(JText::plural('COM_USERS_N_USERS_BLOCKED', count($ids)));
				}
				elseif ($value == 0)
				{
					$this->setMessage(JText::plural('COM_USERS_N_USERS_UNBLOCKED', count($ids)));
				}
			}
		}

		$this->setRedirect('index.php?option=com_users&view=users');
	}

	/**
	 * Method to activate a record.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	public function activate()
	{
		//echo "dds" ;die();
		// Check for request forgeries.
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$ids	= JRequest::getVar('cid', array(), '', 'array');

		if (empty($ids))
		{
			
			JError::raiseWarning(500, JText::_('COM_USERS_USERS_NO_ITEM_SELECTED'));
		}
		else
		{
			
		   foreach($ids as $id)
			{
				$db =& JFactory::getDBO();
				
				$query="update #__users set block=0 ,activation='' where id=".$id."";
				$db->setQuery($query);
				$db->query();
				
				$query="select * from #__user_usergroup_map,#__users where user_id=id and user_id=".$id." ";
				$db->setQuery( $query );
                $id_group=$db->loadObject()->group_id;
                $user=$db->loadObject();
                if($id_group==9)
                {
                	    //echo "dd" ;die();
                	    $app		= JFactory::getApplication();
                	    $mail = JFactory::getMailer();
						$mailfrom	= $app->getCfg('mailfrom');
						$fromname	= $app->getCfg('fromname');
						$sitename	= $app->getCfg('sitename');
						$mail->IsHTML(true);
						$mail->addRecipient($db->loadObject()->email);
						//echo $db->loadObject()->username ;die();
						$mail->setSender(array($mailfrom, $fromname));
						$mail->setSubject('Download Your LogPoint Trial');
						 $body="<div style=\"font-family:  'Lato', Arial, Helvetica, sans-serif;\">
				Dear $user->first_name $user->last_name,<br><br> 
			    Thank you for requesting a LogPoint trial!<br><br> 
			    Download<br>
			    You can download the trial version of LogPoint by clicking on this link:<Br>
			    <a href='http://www.immunesecurity.com/DOWNLOAD/LogInspect.ova'>LogPoint 5.1 Trial</a><br><br> 
			    The demo appliance can be executed from any virtualization platform that supports the ova format and is capable of running Ubuntu 10.04 LTS.<br><br>
			    <b>Installation and Activation</b><br>
			    The links below contain an installation guide and the relevant manual, which will help you get a good start. <br><br>
			    <a href='http://www.immunesecurity.com/DOWNLOAD/QuickInstallGuideV5.pdf'>Quick Install Guide</a><br><br>
			    <a href='http://www.immunesecurity.com/DOWNLOAD/ManualPack.zip'>LogPoint 5.1 manual pack (encrypted)</a><br><br>
			    <a href='http://www.immunesecurity.com/DOWNLOAD/DEMO/license.pak'>LogPoint 30 days trial license</a><br><br>
			    We hope you enjoy the trial version. If you have any questions, please feel free to contact our support team.<br><br>
			    Best regards<br><br> 
			    <img src='http://logpoint.server1881383095.internet-server.dk/images/mail_logo.png'/><br>
			    LogPoint A/S<br>
			    Aldersrogade 6A<br>
			    2100 Copenhagen O<br>
			    Denmark<br><br>
			    Telephone	+45 7026 6286<br>
			    E-mail	<a href='mailto:support@logpoint.com'>support@logpoint.com</a><br>
			    Web	<a href='www.logpoint.com'>www.logpoint.com</a></div>
                ";
						$mail->setBody($body);
						$sent = $mail->Send();
                }
                elseif($id_group==2)
                {
                	 $app		= JFactory::getApplication();
                	    $mail = JFactory::getMailer();
						$mailfrom	= $app->getCfg('mailfrom');
						$fromname	= $app->getCfg('fromname');
						$sitename	= $app->getCfg('sitename');
						$mail->IsHTML(true);
						$mail->addRecipient($db->loadObject()->email);
						//echo $db->loadObject()->username ;die();
						$mail->setSender(array($mailfrom, $fromname));
						$mail->setSubject('Your LogPoint account is active!');
						$link = JURI::root().'index.php?show_login=1';
						 $body="<div style=\"font-family:  'Lato', Arial, Helvetica, sans-serif;\">
				Dear $user->first_name $user->last_name,<br><br> 
			    Your Partner account has been activated for the LogPoint Partner Community. <br><br>  
				You are now able to <a href='$link'>login</a> using this e-mail address and the password that you entered when you signed up.<br><br>   
			    Best regards<br><br> 
			    <img border='0' width='100' height='60' src='http://logpoint.server1881383095.internet-server.dk/images/mail_logo.png'/><br>
			    LogPoint A/S<br>
			    Aldersrogade 6A<br>
			    2100 Copenhagen O<br>
			    Denmark<br><br>
			    Telephone	+45 7026 6286<br>
			    E-mail	<a href='mailto:support@logpoint.com'>support@logpoint.com</a><br>
			    Web	<a href='www.logpoint.com'>www.logpoint.com</a>
				</div>
                ";
						$mail->setBody($body);
						$sent = $mail->Send();
                }
                else 
                {
                   	 $app		= JFactory::getApplication();
                	    $mail = JFactory::getMailer();
						$mailfrom	= $app->getCfg('mailfrom');
						$fromname	= $app->getCfg('fromname');
						$sitename	= $app->getCfg('sitename');
						$mail->IsHTML(true);
						$mail->addRecipient($db->loadObject()->email);
						//echo $db->loadObject()->username ;die();
						$mail->setSender(array($mailfrom, $fromname));
						$mail->setSubject('Your LogPoint Webinar');
						 $body="<div style=\"font-family:  'Lato', Arial, Helvetica, sans-serif;\">
						 Dear $user->name,<br><br> 
						Thank you for signing up for one of LogPoint’s webinars. <br><br> 
						Best regards<br><br> 
						<img border='0' width='100' height='60' src='http://logpoint.server1881383095.internet-server.dk/images/mail_logo.png'/><br>
						LogPoint A/S<br>
						Aldersrogade 6A<br>
						2100 Copenhagen O<br>
						Denmark<br><br>
						Telephone	+45 7026 6286<br>
						E-mail	<a href='mailto:support@logpoint.com'>info@logpoint.com</a><br>
						Web	<a href='www.logpoint.com'>www.logpoint.com</a></div>
                ";
						$mail->setBody($body);
						$sent = $mail->Send();
                }
			}
			
			
			// Get the model.
			$model = $this->getModel();

			// Change the state of the records.
			if (!$model->activate($ids))
			{
				$this->setMessage(JText::plural('COM_USERS_N_USERS_ACTIVATED', count($ids)));
				//JError::raiseWarning(500, $model->getError());
			}
			else
			{
				$this->setMessage(JText::plural('COM_USERS_N_USERS_ACTIVATED', count($ids)));
			}
		}

		$this->setRedirect('index.php?option=com_users&view=users');
	}
}
